package com.glorysoft.wangnianxinpda.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.glorysoft.wangnianxinpda.R;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_TextCallback;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabIndexGridEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;

import java.util.List;

/**
 * @author hx
 * @date 2019/11/15
 * @desc
 */
public class MyUtils {
    public final int MIN_DELAY_TIME = 1000; // 两次点击间隔不能少于1000ms
    public long lastClickTime = 0l;
    public boolean isFastClick = true;

    public boolean isFastClick() {
        long currentTimeMillis = System.currentTimeMillis();
        if (currentTimeMillis - lastClickTime >= MIN_DELAY_TIME) {
            isFastClick = false;
        }
        lastClickTime = currentTimeMillis;
        return isFastClick;
    }


    public interface EditTextActionListener {
        void onKeyDown(String s);
    }

    public static List<FreeUI_TabIndexGridEntity> setPicToDrawable(Context context, List<FreeUI_TabIndexGridEntity> entity) {
        for (FreeUI_TabIndexGridEntity og : entity) {
            if (og.getPicName().length() != 0) {
                Drawable drawable = context.getResources()
                        .getDrawable(context.getResources()
                                .getIdentifier(og.getPicName(), "drawable", context.getPackageName()));
                og.setDrawable(drawable);
            }
        }
        return entity;
    }

    public static void editActionListener(final EditText editText, final EditTextActionListener listener) {
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                //当actionId == XX_SEND 或者 XX_DONE时都触发
                //或者event.getKeyCode == ENTER 且 event.getAction == ACTION_DOWN时也触发
                if (actionId == EditorInfo.IME_ACTION_SEND
                        || actionId == EditorInfo.IME_ACTION_NEXT
                        || actionId == EditorInfo.IME_ACTION_DONE
                        || (keyEvent != null && KeyEvent.KEYCODE_ENTER == keyEvent.getKeyCode() && KeyEvent.ACTION_DOWN == keyEvent.getAction())) {
                    //处理事件
                    listener.onKeyDown(editText.getText().toString().trim());
                }
                return false;
            }
        });
    }

    public static void setEditTextListener(EditText editText, Context context, FreeUI_TextCallback callback) {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //确保只能扫描或粘贴触发该方法
                if (start == 0 && before == 0 && count > 1) {
                    if (s.length() > 0) {
                        callback.onSuccess(s.toString());
                    } else {
                        FreeApi_ToastUtils.showFreeToast(context.getString(R.string.scanInfoError), context, false, Toast.LENGTH_LONG);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    //监听编辑框的输入
    public static void setNewEditTextListener(EditText editText, Context context, FreeUI_TextCallback callback) {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                callback.onSuccess(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

}
